package controller

import (
	"gin-service/lib/GinSrv/GinResp"
	"gin-service/lib/GinSrv/GinRouter"
	"gin-service/lib/GormSrv"
	"gin-service/lib/GormSrv/model"
	"gin-service/utils"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
)

//erc的销售管理模块
func ErcCustomerController(router GinRouter.Router) {
	router.InitRouter()
	response := new(GinResp.RespImpl)

	//erc客户向供应商的采购订单
	//user客户 client供应商
	router.Get("/getPurchaseOrder", func(ctx *gin.Context) {
		filter := bson.M{}

		clientCode := ctx.Query("pivotClientCode")
		if clientCode != "" {
			filter["pivot_client_code"] = clientCode
		}

		userCode := ctx.Query("userCode")
		if userCode != "" {
			filter["pivot_user_code"] = userCode
		}

		finishState := utils.STI(ctx.Query("finishState"))
		if finishState > 0 {
			filter["finish_state"] = finishState
		}

		resultData := GinRouter.MongoQuery(ctx, "purchase_order_item", filter, nil)
		response.DataResp(ctx, resultData)
	})

	//erc客户给供应商的发货命令
	//user客户 client供应商
	router.Get("/getSupplyMaterielPlan", func(ctx *gin.Context) {
		filter := bson.M{}

		clientCode := ctx.Query("clientCode")
		if clientCode != "" {
			filter["pivot_client_code"] = clientCode
		}

		userCode := ctx.Query("userCode")
		if userCode != "" {
			filter["pivot_user_code"] = userCode
		}

		resultData := GinRouter.MongoQuery(ctx, "supply_materiel_plan", filter, nil)
		response.DataResp(ctx, resultData)
	})

	router.Post("/post/:id", func(ctx *gin.Context) {
		demoId := ctx.Param("demoId")
		queryStr := ctx.Query("query")

		type Payload struct {
			Name string
			Age int
		}

		payload := Payload{}
		err := ctx.BindJSON(&payload)
		if err != nil {
			panic(err.Error())
		}

		response.DataResp(ctx, gin.H{
			"demoId": demoId,
			"queryStr": queryStr,
			"name": payload.Name,
			"age": payload.Age,
		})
	})

	router.Post("/insert", func(ctx *gin.Context) {
		type Payload struct {
			Name string
		}

		payload := Payload{}
		err := ctx.BindJSON(&payload)
		if err != nil {
			panic(err.Error())
		}

		demo := model.Demo{DemoName: payload.Name}
		result := GormSrv.GDB().Create(&demo)
		GormSrv.ThrowDBError(result)

		response.DataResp(ctx, demo)
	})

	router.Put("/update", func(ctx *gin.Context) {
		response.DataResp(ctx, gin.H{
			"message": "update",
		})
	})
	
	router.Delete("/delete", func(ctx *gin.Context) {
		response.DataResp(ctx, gin.H{
			"message": "delete",
		})
	})
}
